<script setup lang='ts'>
  import { onMounted, reactive } from 'vue';
  import { propApi } from '@/api/controller';
  import { Toastloading, ToastWarning } from '@/utils/public';
  import { getPlayEndTime, getDateArray } from '@/utils'
  import Pagination from "@/components/Pagination/index.vue";
  import xmDatePicker from "@/components/xmDatePicker/index.vue";
  import table2excel from 'js-table2excel';

  const action = reactive({
    total:0,
    list:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymdhis'))

  })
  const listQuery = reactive({
    user_uuid:'',
    user_id:'',
    // room_name:'',
    page:1,
    limit:20,
  })
  const handleSerach = () => {
    listQuery.page = 1;
    getList();
  };
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中...')
    const query = { ...listQuery }
    query.page = 1
    query.limit = action.total || 20
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await propApi.geteastereggRecordingList({
      ...query,
      date_st,
      date_ed
    })
    loading.close()
    const { data=[]} = res.data
    loading.close()
    if(data.length == 0) {
      ToastWarning('暂无可导出数据')
      return
    }
    const tHeader = [
      { title: 'ID', key: "id"},
      { title: '送礼人ID', key: "user_id"},
      { title: '送礼人昵称', key: "send_user_name"},
      { title: '收礼人ID', key: "to_user_id"},
      { title: '收礼人昵称', key: "to_user_name"},
      { title: '房间ID', key: "room_id"},
      // { title: '房间名称', key: "room_name"},
      { title: '彩蛋ID', key: "color_id"},
      { title: '彩蛋名称', key: "color_gift_name"},
      { title: '爆出数量', key: "gift_num"},
      { title: '爆出礼物ID', key: "award_gift_id"},
      { title: '返奖礼物', key: "award_gift_name"},
      { title: '返奖价值(钻)', key: "award_gift_price"},
      { title: '钻石消耗', key: "cost_diamond"},
      { title: '收入(钻)', key: "reward_money"},
      { title: '日期', key: "created_at"},
    ]
    table2excel(tHeader, data, '彩蛋爆出记录')
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await propApi.geteastereggRecordingList({
      ...listQuery,
      date_st,
      date_ed
    })
    loading.close()
    const { data=[], total=0, } = res?.data || {}
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">送礼人UUID：</div>
          <el-input v-model="listQuery.user_uuid" placeholder="送礼人UUID" />
        </div>
        <div class="labelItem">
          <div class="label">送礼人ID：</div>
          <el-input v-model="listQuery.user_id" placeholder="送礼人ID" />
        </div>
<!--        <div class="labelItem">-->
<!--          <div class="label">房间名称：</div>-->
<!--          <el-input v-model="listQuery.room_name" placeholder="房间名称" />-->
<!--        </div>-->
        <xmDatePicker v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">搜索</el-button>
          <el-button type="warning" @click="handleDownload">导出</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="id" label="ID" />
      <el-table-column width="100" prop="user_id" label="送礼人ID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="send_user_name" label="送礼人昵称" />
      <el-table-column width="100" prop="to_user_id" label="收礼人ID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="to_user_name" label="收礼人昵称" />
      <el-table-column width="100" prop="room_id" label="房间ID" />
<!--      <el-table-column width="100" prop="room_name" label="房间名称" />-->
      <el-table-column width="100" prop="color_id" label="彩蛋ID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="color_gift_name" label="彩蛋名称" />
      <el-table-column width="100" sortable prop="gift_num" label="爆出数量" />
      <el-table-column width="100" prop="award_gift_id" label="爆出礼物ID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="award_gift_name" label="返奖礼物" />
      <el-table-column width="100" sortable prop="award_gift_price" label="返奖价值(钻)" />
      <el-table-column width="100" sortable prop="cost_diamond" label="钻石消耗" />
      <el-table-column width="100" sortable prop="reward_money" label="收入(钻)" />
      <el-table-column width="180" prop="created_at" label="日期" />

    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
  </div>
</template>
